package com.xiaoerkeji.eshop.ui.dialog;


import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;

import com.xiaoerkeji.eshop.R;
import com.xiaoerkeji.eshop.databinding.DialogFileDownBinding;
import com.xiaoerkeji.eshop.listener.MyInterface;
import com.lt.base.BaseDialog;
import com.lt.common.ConvertHelper;
import com.lt.phone.ScreenHelper;

import java.util.Objects;

/**
 * 升级-下载文件对话框
 *
 * @author luotian
 */
public class DialogFileDown extends BaseDialog
{
    private MyInterface.OnDialogCountDownListener listener = null;
    private long totalSize = 0;
    int progress = 0;  //完成百分比
    private DialogFileDownBinding binding;

    public DialogFileDown(Activity act)
    {
        super(act, R.style.dialog_my);
        this.mActivity = act;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        binding = DialogFileDownBinding.inflate(getLayoutInflater());
        Objects.requireNonNull(getWindow()).setContentView(binding.getRoot());
        getWindow().setGravity(Gravity.BOTTOM);
        int width = ScreenHelper.getScreenWidth();
        setDialogSize(width, (width * 300) / 420);
    }

    public void show(String version, String content, MyInterface.OnDialogCountDownListener listener)
    {
        if (showDialog())
        {
            this.listener = listener;
            if (TextUtils.isEmpty(version))
            {
                binding.tvVersion.setText("未知");
            } else
            {
                binding.tvVersion.setText(version);
            }
            if (TextUtils.isEmpty(content))
            {
                binding.tvContent.setText("修复已知问题");
            } else
            {
                binding.tvContent.setText(content);
            }
        }
    }

    public void closeSlef()
    {
        if (this.isShowing())
        {
            dismiss();
        }
    }

    public void updateView(long finishedSize, long totalSize)
    {
        if (!isShow())
        {
            return;
        }
        if (this.totalSize == 0)
        {
            this.totalSize = totalSize;
            binding.tvSize.setText(ConvertHelper.getInstance().simpleFormatSize(totalSize));
        }
        if (totalSize != 0)
        {
            int progresstmp = (int) ((finishedSize * 100) / totalSize);
            if (this.progress != progresstmp)
            {
                this.progress = progresstmp;
                binding.progressbar.setProgress(progress);
            }
        }
    }

    public void start()
    {
        binding.progressbar.setProgress(0);
    }


    public void complete()
    {
        binding.progressbar.setProgress(100);
    }

}
